package services

import (
	"beego-admin/models"
	"encoding/json"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
)


//用户
type Details struct {
	Id 		int `json:"id"`
	Title 	string  `json:"title"`
	Summary string  `json:"summary"`
	Author 	string  `json:"author"`
}

// UserProfileService struct
type UserProfileService struct {
	BaseService
}

// Create 新增User Profile数据
func (*UserProfileService) Create(form *models.UserProfile) int {
	if form==nil{
		return 0
	}else{
		id, err := orm.NewOrm().Insert(form)
		fmt.Println(err)
		if err == nil {
			return int(id)
		}else {
			return 0
		}
	}
}

// GetAllUserProfile 获取数据
func (*UserProfileService) GetAllAdminUser() []*models.UserProfile {
	var userProfile []*models.UserProfile

	_, err := orm.NewOrm().QueryTable(new(models.UserProfile)).Filter("id", 1).All(&userProfile) //指定查询
	//_, err := orm.NewOrm().QueryTable(new(models.UserProfile)).Filter("id__in", 1,2,3).All(&userProfile) //in查询
	//_, err := orm.NewOrm().QueryTable(new(models.UserProfile)).Filter("profile__contains", "血质").All(&userProfile) //startswith/istartswith/endswith/iendswith 模糊查询

	//_, err := orm.NewOrm().QueryTable(new(models.UserProfile)).Filter("id__in", 18,19,20).Filter("profile__contains", "血质").All(&userProfile) //多字段查询
	//_, err := orm.NewQueryBuilder("mysql").Select("user.name", "profile.age").From("user").InnerJoin("profile").On("user.id_user = profile.fk_user").Where("age > ?").OrderBy("name").Desc().Limit(10).Offset(0)

	//cond := orm.NewCondition()//自定义条件查询
	////cond1 := cond.And("profile__isnull", false).AndNot("status__in", 1).Or("profile__age__gt", 2000)
	//_, err := orm.NewOrm().QueryTable(new(models.UserProfile)).SetCond(cond.And("id__in", 1,2,3).Or("profile__contains", "血质")).All(&userProfile) //多字段查询

	//_, err := orm.NewOrm().QueryTable(new(models.UserProfile)).Limit(2).GroupBy("age").All(&userProfile)  //排序，限制，分组，去重Distinct ，Count计数


	//_, err := orm.NewOrm().QueryTable(new(models.UserProfile)).RelatedSel().Filter("Posts", 8).All(&userProfile)


	//_, err := orm.NewOrm().QueryTable(new(models.UserProfile)).Filter("id__in", 18,19,20).Update(orm.Params{
	//	"age": 100,
	//})  更新数据

	//_, err := orm.NewOrm().QueryTable(new(models.UserProfile)).Filter("id__in", 18,19,20).Delete()

	//2021/08/20 联合查询 后台页面的增加
	if err != nil {
		return nil
	}
	return userProfile
}



// GetAllUser 多表联合查询
func (*UserProfileService) GetAllUser() interface{} {
	qb, _ := orm.NewQueryBuilder("mysql")

	// 构建查询对象
	sql := qb.Select("user.*","user_profile.*").From("user").InnerJoin("user_profile").On("user.id = user_profile.posts").Where("user.id = 8").OrderBy("user.id").Desc().Limit(30).String()
	// 导出SQL语句
	//sql := qb.String()
	type Params map[string]interface{}
	var lists []orm.Params
	// 执行SQL语句
	num, err := orm.NewOrm().Raw(sql).Values(&lists)

	//fmt.Println(lists)
	if err != nil  && num > 0{
		return nil
	}else {
		//json 解析
		for key, value := range lists {
			var Details Details
			numData := json.Unmarshal([]byte(value["profile"].(string)), &Details)
			if numData!=nil{
				fmt.Println(err)
			}
			lists[key]["profile"] = Details
			fmt.Println(Details)
		}

	}

	return lists

}


